﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DBBase;
using System.Threading;
using System.Xml;


using System.IO;

using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using System.Security;
using System.Security.Cryptography;

namespace Business
{
    public partial class Audit_Core : Form
    {
        public MainForm m_mainform;
        public Audit_Core()
        {
            InitializeComponent();
        }

      
        private string Encryption(string express)//加密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] plaindate = Encoding.Default.GetBytes(express);
                byte[] encryptdate = rsa.Encrypt(plaindate, false);

                return Convert.ToBase64String(encryptdate);
            }
        }
        //
        private string Decrypt(string cip)//解密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] encryptdate = Convert.FromBase64String(cip);
                byte[] decryptdate = rsa.Decrypt(encryptdate, false);
                return Encoding.Default.GetString(decryptdate);
            }
        }
        public void getjilu()
        {
            lv.Items.Clear();
            Query mquery = new Query();
            string sql;
            sql = string.Format(@"Select * from hexinriji Where shrj_sjsj>'{0}' and shrj_sjsj<'{1}'", dateTimePicker1.Value.ToShortDateString() + " 00:00:00", dateTimePicker2.Value.ToShortDateString() + " 23:59:59");

            sql = sql + " order by shrj_id desc ";
            //保存操作日记
            m_mainform.savecaozuoriji(sql, "查询", "业务系统", "查询核心功能审计");
            //try
            {
                DataTable dt = mquery.GetTable(sql);

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                      
                        ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                        lt.SubItems.Add(dt.Rows[i]["shrj_aqcl"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["shrj_yhdh"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["shrj_yhip"].ToString().Trim());
                        string rjnr = dt.Rows[i]["shrj_rjnr"].ToString().Trim();
                        try
                        {
                            rjnr = Decrypt(dt.Rows[i]["shrj_rjnr"].ToString().Trim());
                        }
                        catch
                        {
                            rjnr = rjnr + "*";
                        }
                        lt.SubItems.Add(rjnr);
                       // lt.SubItems.Add(dt.Rows[i]["shrj_rjnr"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["shrj_sjsj"].ToString().Trim());

                        this.lv.Items.Add(lt);

                    }
                    foreach (ListViewItem lvItems in lv.Items)
                    {
                        if (lvItems.SubItems[4].Text.Contains("*"))
                        {
                            lvItems.ForeColor = Color.Red;//字体颜色
                        }
                        else
                        {
                            lvItems.ForeColor = Color.Black;//字体颜色
                        }
                    }
                }


            }



        }

        private void button2_Click(object sender, EventArgs e)
        {
           
        }
        private void excel(string path)
        {
            try
            {
                StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
                StringBuilder sb = new StringBuilder();
                // sb.Append("地区名" + "\t");
                sb.Append("统计时间从" + dateTimePicker1.Value.ToShortDateString() + "至" + dateTimePicker2.Value.ToShortDateString() + "\t");
                sb.Append(Environment.NewLine);
                for (int i = 0; i < lv.Columns.Count; i++)
                    sb.Append(lv.Columns[i].Text.ToString() + "\t");
                sb.Append(Environment.NewLine);

                foreach (ListViewItem li in lv.Items)
                {
                    for (int i = 0; i < lv.Columns.Count; i++)
                        sb.Append(li.SubItems[i].Text + "\t");
                    sb.Append(Environment.NewLine);
                }
                sw.Write(sb.ToString());
                sw.Flush();
                sw.Close();
                MessageBox.Show("成功保存于" + path + "文件！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       

        private void but_query_Click(object sender, EventArgs e)
        {
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("核心功能审计"))
            {
                m_mainform.baocunhxrj("核心功能审计", string.Format("核心功能审计"), "查询");// 核心功能日记  日记内容
            }
            getjilu();
        }

        private void but_tongji_Click(object sender, EventArgs e)
        {
            lv.Items.Clear();
            Query mquery = new Query();
            string sql;
            sql = string.Format(@"Select * from hexinriji Where shrj_sjsj>'{0}' and shrj_sjsj<'{1}'", dateTimePicker1.Value.ToShortDateString() + " 00:00:00", dateTimePicker2.Value.ToShortDateString() + " 23:59:59");
            if (textBox1.Text != "")
            {
                sql = sql + string.Format(@" and  shrj_yhdh='{0}' ", textBox1.Text);
            }
            sql = sql + " order by shrj_id desc ";
            //try
            {
                DataTable dt = mquery.GetTable(sql);

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {

                        ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                        lt.SubItems.Add(dt.Rows[i]["shrj_aqcl"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["shrj_yhdh"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["shrj_yhip"].ToString().Trim());
                        string rjnr = dt.Rows[i]["shrj_rjnr"].ToString().Trim();
                        try
                        {
                            rjnr = Decrypt(dt.Rows[i]["shrj_rjnr"].ToString().Trim());
                        }
                        catch
                        {
                            rjnr = rjnr + "*";
                        }
                        lt.SubItems.Add(rjnr);
                        // lt.SubItems.Add(dt.Rows[i]["shrj_rjnr"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["shrj_sjsj"].ToString().Trim());

                        this.lv.Items.Add(lt);

                    }
                    foreach (ListViewItem lvItems in lv.Items)
                    {
                        if (lvItems.SubItems[4].Text.Contains("*"))
                        {
                            lvItems.ForeColor = Color.Red;//字体颜色
                        }
                        else
                        {
                            lvItems.ForeColor = Color.Black;//字体颜色
                        }
                    }
                }


            }
        }

        private void but_daochu_Click(object sender, EventArgs e)
        {
            string path = "";
            path = Application.StartupPath + path + DateTime.Now.ToString("yyyyMMdd") + ".xls";
            excel(path);
        }

        private void Audit_Core_Load(object sender, EventArgs e)
        {
            //判断是否民警
            if (m_mainform.sfjy.Contains("核心功能审计"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_query.Enabled = false;
                }
            }
        }
    }
}
